package com.wink.admin.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author xw
 * @date 2020-10-27 18:24
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private ValueOperations<String, Object> getStringTemplate() {
        return redisTemplate.opsForValue();
    }

    private HashOperations<String, Object, Object> getHashTemplate() {
        return redisTemplate.opsForHash();
    }

    private ListOperations<String, Object> getListTemplate() {
        return redisTemplate.opsForList();
    }

    private SetOperations<String, Object> getSetTemplate() {
        return redisTemplate.opsForSet();
    }

    private ZSetOperations<String, Object> getZSetTemplate() {
        return redisTemplate.opsForZSet();
    }

    @Override
    public Object get(String key) {
        return getStringTemplate().get(key);
    }

    @Override
    public void set(String key, Object value, Long liveSeconds) {
        getStringTemplate().set(key, value);
    }

    @Override
    public void set(String key, Object value) {
        getStringTemplate().set(key, value);
    }

    @Override
    public Boolean setnx(String key, Object value) {
        return getStringTemplate().setIfAbsent(key, value);
    }

    @Override
    public Boolean setnx(String key, Object value, Long liveSeconds) {
        return getStringTemplate().setIfAbsent(key, value, liveSeconds, TimeUnit.SECONDS);
    }

    @Override
    public Object getAndSet(String key, Object value) {
        return getStringTemplate().getAndSet(key, value);
    }

    @Override
    public void hset(String key, Object hashKey, Object value) {
        getHashTemplate().put(key, hashKey, value);
    }


    @Override
    public void hset(String key, Map<Object, Object> values) {
        getHashTemplate().putAll(key, values);
    }

    @Override
    public Object hget(String key, Object hashKey) {
        return getHashTemplate().get(key, hashKey);
    }

    @Override
    public List<Object> hget(String key, Collection<Object> hashKeys) {
        return getHashTemplate().multiGet(key, hashKeys);
    }

    @Override
    public Boolean hasHashKey(String key, Object hashKey) {
        return getHashTemplate().hasKey(key, hashKey);
    }

    @Override
    public Long hsize(String key) {
        return getHashTemplate().size(key);
    }

    @Override
    public void hdelete(String key, Object... hashKeys) {
        getHashTemplate().size(key);
    }

    @Override
    public void lPush(String key, String value) {
        getListTemplate().leftPush(key, value);
    }

    @Override
    public void lPush(String key, Collection<Object> values) {
        getListTemplate().leftPush(key, values);
    }

    @Override
    public Object lRPop(String key, long timeout, TimeUnit unit) {
        return getListTemplate().rightPop(key, timeout, unit);
    }

    @Override
    public Set<Object> sget(String key) {
        return getSetTemplate().members(key);
    }

    @Override
    public Long sadd(String key, Object... values) {
        return getSetTemplate().add(key, values);
    }

    @Override
    public Long sdelete(String key, Object... values) {
        return getSetTemplate().remove(key, values);
    }

    @Override
    public List<Object> sdeleteRandom(String key, long count) {
        return getSetTemplate().randomMembers(key, count);
    }

    @Override
    public Boolean hasSetValue(String key, Object value) {
        return getSetTemplate().isMember(key, value);
    }

    @Override
    public Double zgetVscore(String key, Object value) {
        return getZSetTemplate().score(key, value);
    }

    @Override
    public Boolean zadd(String key, Object value, double score) {
        return getZSetTemplate().add(key, value, score);
    }

    @Override
    public Set<Object> range(String key, long start, long end) {
        return getZSetTemplate().range(key, start, end);
    }

    @Override
    public Set<Object> rangeByScore(String key, double min, double max) {
        return getZSetTemplate().rangeByScore(key, min, max);
    }

    @Override
    public Set<Object> rangeByScore(String key, double min, double max, long start, long end) {
        return getZSetTemplate().rangeByScore(key, min, max, start, end);
    }

    @Override
    public Long count(String key, double min, double max) {
        return getZSetTemplate().zCard(key);
    }

    @Override
    public Long zsize(String key) {
        return getZSetTemplate().size(key);
    }

    @Override
    public Long zdelete(String key, Object... values) {
        return getZSetTemplate().remove(key, values);
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public void deletes(Collection<String> keys) {
        redisTemplate.delete(keys);
    }

    @Override
    public void deletePrefix(String preKay) {
        Set<String> keys = redisTemplate.keys(preKay.concat("**"));
        if (keys != null && keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    @Override
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    @Override
    public Boolean expire(String key, Long liveSeconds) {
        return redisTemplate.expire(key, liveSeconds, TimeUnit.SECONDS);
    }

    @Override
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public Long increment(String key, long value) {
        return redisTemplate.boundValueOps(key).increment(value);
    }

    @Override
    public Long increment(String key) {
        return redisTemplate.boundValueOps(key).increment(1L);
    }

    @Override
    public void sendMessage(String channel, Serializable message) {
        redisTemplate.convertAndSend(channel, message);
    }
}
